package cm.cool.web.service.price;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;

import cm.cool.system.bean.PriceDto;
import cm.cool.system.request.HttpRequestor;
import cm.cool.web.bean.CurrencyPrice;
import cm.cool.web.data.CurrencyPriceMapper;
import cm.cool.web.util.respon.PaginationDto;
import cm.cool.web.util.respon.R;

@Service
public class PriceService {
	
	
	@Autowired
	CurrencyPriceMapper currencyPriceMapper;
	
	@Autowired
	PriceDto priceDto;
	
	/**
	 * 定时更新币种情况的service类
	 * 接口返回的json格式:
	 * {"date":"1508816528","ticker":{"high":"360.0","vol":"267331.047","last":"351.98","low":"318.0","buy":"349.22","sell":"351.98"}}
	 */
	public void  updatePrice(){
		
		try {
			
			String[] keyTypes=PriceDto.getPriceDto().getKeyType().split(",");
			String[] valetTypes=PriceDto.getPriceDto().getValetType().split(",");
			
			for(int i=keyTypes.length-1;i>=0;i--){//遍历需要查询的所有币种信息
				
				CurrencyPrice currencyPrice=new CurrencyPrice();
				String url="https://www.bitstamp.net/api/v2/ticker_hour/"+keyTypes[i];
				String result=HttpRequestor.doGet(url);
				//解析相关信息
				JSONObject resultJson= JSONObject.parseObject(result);
				//组装相关信息
				currencyPrice.setTimestamp(paserTime(Long.parseLong(resultJson.getString("timestamp"))));
				currencyPrice.setOpen(resultJson.getString("open"));                        
				currencyPrice.setHigh(resultJson.getString("high"));
				currencyPrice.setIdCode(keyTypes[i]);
				currencyPrice.setLast(resultJson.getString("last"));
				currencyPrice.setLow(resultJson.getString("low"));
				currencyPrice.setVolume(resultJson.getString("volume"));
				currencyPrice.setAsk(resultJson.getString("ask"));
				currencyPrice.setVwap(resultJson.getString("vwap"));
				currencyPrice.setBid(resultJson.getString("bid"));
				currencyPrice.setCurrencyName(valetTypes[i]);
				currencyPriceMapper.addOrupdate(currencyPrice);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	  /* 
     * 将时间戳转换为时间
     */
    public static String paserTime(long time){  
        System.setProperty("user.timezone", "Asia/Shanghai");  
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");  
        TimeZone.setDefault(tz);  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String times = format.format(new Date(time * 1000L));  
        System.out.print("日期格式---->" + times);  
        return times;  
  }  
    
    /**
     * 条件超找所有的货币信息
     * @param currencyPrice
     * @return
     */
    public R selectAll(CurrencyPrice currencyPrice,PaginationDto paginationDto){
    	R r=new R();
    	
    	try {
    		PageHelper.startPage(paginationDto.getCurrentPageNum(), paginationDto.getPerPageSize());
			paginationDto.setEntitys(currencyPriceMapper.selectByAll(currencyPrice));
			int count = currencyPriceMapper.selectByAllCount(currencyPrice);
			paginationDto.setTotalCount(count);
			paginationDto.setTotalPageNum((int)Math.ceil((count*1.0) / paginationDto.getPerPageSize()));
			r.setData(paginationDto);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	return r;
    }
}
